package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.utils.AppUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 商城留言activity
 */

public class ShoppingMessage extends AppCompatActivity implements View.OnClickListener{

    private ImageView secondary_return;
    private EditText comment;
    private Button submit;
    String bookId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_message);
        secondary_return = (ImageView) findViewById(R.id.secondary_return);
        comment = (EditText) findViewById(R.id.comment);
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        secondary_return.setOnClickListener(this);
        bookId = getIntent().getStringExtra("bookId");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.secondary_return:
                finish();
                break;
            case R.id.submit:
                if (TextUtils.isEmpty(comment.getText())) {
                    Toast.makeText(ShoppingMessage.this, "留言不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                submit.setEnabled(false);
                String url = AppUrl.URL + AppUrl.LeaveMessage;
                Map<String,String> map = new HashMap<>();
                map.put("replyUid", ApplicationUtils.getInstance().loginBean.getConsumerid());
                map.put("replybookId", bookId);
                map.put("replyComment", comment.getText().toString());
                Log.e("id",ApplicationUtils.getInstance().loginBean.getConsumerid()+"==replyUid------replybookId=="+bookId+"--------replyComment=="+comment.getText().toString());
                XUtils.Get(url,map,new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            boolean data = jsonObject.optBoolean("result");
                            if(data==true){
                                Toast.makeText(ShoppingMessage.this, "留言成功，将尽快给您回复", Toast.LENGTH_SHORT).show();
                                finish();
                            }else{
                                Toast.makeText(ShoppingMessage.this, "留言失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                    }

                    @Override
                    public void onFinished() {
                        super.onFinished();
                    }
                });
                break;
        }
    }
}
